/*
 * PACKAGE NAME: com.jodinfo.outmirror.api.offmessage.service
 * FILE    NAME: OffMessageDaoImpl.java
 * COPYRIGHT: Copyright(c) © 2016 heroj Company Ltd. All Rights Reserved
 */ 
package com.jodinfo.outmirror.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.jodinfo.outmirror.entity.OffMessageEntity;
import com.jodinfo.outmirror.service.OffMessageBizService;
import com.jodinfo.outmirror.service.OffMessageDmlService;
import com.jodinfo.outmirror.service.OffMessageQueryService;
import com.jodinfo.outmirror.util.PageResult;
import com.jodinfo.outmirror.util.ResponseEntity;
import com.jodinfo.outmirror.util.StringUtils;
import com.jodinfo.outmirror.util.SysConstant;

@Service(value="offMessageBizService")
public class OffMessageBizServiceImpl implements OffMessageBizService {
    private static final Logger logger = LoggerFactory.getLogger(OffMessageBizServiceImpl.class);
 	
 	@Resource(name="offMessageDmlService")
	private OffMessageDmlService offMessageDmlService;
	@Resource(name="offMessageQueryService")
	private OffMessageQueryService offMessageQueryService;
	
	/** 
	 * 
	 * @author ZhangYuanYuan
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.offmessage.OffMessageBizService#addOffMessage(OffMessageEntity offMessageEntity)
	 * @param OffMessageEntity offMessageEntity
	 * @return int
	 */
	public ResponseEntity addOffMessage(OffMessageEntity offMessageEntity) {
			ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
			  	 int i =  offMessageDmlService.addOffMessage(offMessageEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(Exception e){
				String errorMessage = e.getMessage();
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author ZhangYuanYuan
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.offmessage.OffMessageBizService#updateOffMessage(OffMessageEntity offMessageEntity)
	 * @param OffMessageEntity offMessageEntity
	 * @return ResponseEntity
	 */
	public ResponseEntity updateOffMessage(OffMessageEntity offMessageEntity) {
		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
				int i = offMessageDmlService.updateOffMessage(offMessageEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(Exception e){
				String errorMessage = e.getMessage();
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author ZhangYuanYuan
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.offmessage.OffMessageBizService#queryAllOffMessage(OffMessageEntity offMessageEntity)
	 * @param OffMessageEntity offMessageEntity
	 * @return String
	 */
	public String queryAllOffMessage(OffMessageEntity offMessageEntity) {
		return JSON.toJSONString(offMessageQueryService.queryAllOffMessage(offMessageEntity));
	} 
	/** 
	 * 
	 * @author ZhangYuanYuan
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.offmessage.OffMessageBizService#queryAllOffMessage(OffMessageEntity offMessageEntity)
	 * @param OffMessageEntity offMessageEntity
	 * @return PageResult
	 */
	public String queryListForPageOffMessage(OffMessageEntity offMessageEntity) {
		return new PageResult( offMessageQueryService.queryListForPageOffMessage(offMessageEntity), offMessageQueryService.queryCountOffMessage(offMessageEntity)).toString();
	} 
	/** 
	 * 
	 * @author ZhangYuanYuan
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.offmessage.OffMessageQueryService#queryById(OffMessageEntity offMessageEntity)
	 * @param BigInteger id
	 * @return
	 */
	public OffMessageEntity queryById(BigInteger id) {
		return offMessageQueryService.queryById(id);
	}
	
	public void exportOffMessage(OffMessageEntity offMessageEntity, HttpServletResponse response) {
		List<OffMessageEntity> exportList = null;
		String ids = offMessageEntity.getIds();
		if (StringUtils.isNotEmpty(ids)) {
			exportList =offMessageQueryService.queryAllOffMessage(offMessageEntity);
		} else {
			String[] idArray = ids.split(",");
			List<BigInteger> idList = new ArrayList<BigInteger>();
			for(String id : idArray) {
				idList.add(new BigInteger(id));
			}
			exportList = offMessageQueryService.queryByIds(idList);
			// 将字典中的code转换为name
		   //convertCodeToName(exportList);
		}
		//ExcelExport.exportExcel(exportList, "OffMessageEntityExcel", "OffMessageEntity信息", response);
	}
	
}

